<template>
  <div>
    <p-table
      class="table-list-container"
      :row-key="(record, index) => index"
      :pagination="false"
      :columns="columns"
      :data-source="formatData"
      bordered
    />
  </div>
</template>

<script>
export default {
  props: {
    dataSource: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {};
  },
  computed: {
    formatData() {
      const formatData = this.dataSource;
      if (formatData.length !== 0) {
        formatData.forEach((item) => {
          if (item.project === 1) {
            item.project = '销售小片(Kpcs)';
          } else if (item.project === 2) {
            item.project = '销售大板(Ksht)';
          } else if (item.project === 3) {
            item.project = '营收(CNY亿元)';
          } else {
            item.project = '大板ASP(USD元)';
          }
        });
        const cloneFormatData = JSON.parse(JSON.stringify(formatData));
        console.log('cloneFormatData: ', cloneFormatData);
        cloneFormatData.forEach((item) => {
          delete item.different;
        });
        let data = [];
        cloneFormatData.forEach((item) => {
          const itemNum = Object.values(item).filter((value) => {
            return typeof value === 'number';
          });
          if (itemNum.length) {
            const total = itemNum.reduce((tole, value) => {
              return tole + value;
            });
            data.push(total);
          }
        });

        console.log('data: ', data);
        let count = 0;
        formatData.forEach((item) => {
          Object.assign(item, { total: data[count] });
          count++;
        });
      } else {
        return;
      }
      const revenue = formatData.filter(
        (item) => item.project === '营收(CNY亿元)'
      )[0];
      const revenueKeys = Object.keys(revenue);
      revenueKeys.forEach((item) => {
        if (typeof revenue[item] === 'number') {
          revenue[item] = (revenue[item] / 100000000).toFixed(10);
        }
      });
      return formatData;
    },
    columns() {
      if (this.dataSource.length !== 0) {
        const first = this.dataSource[0];
        const arr = [];
        Object.keys(first).forEach((key) => {
          if (key === 'project') {
            arr.unshift({
              align: 'center',
              title: '项目',
              dataIndex: 'project',
              width: 200,
            });
          } else if (key.indexOf('-') !== -1) {
            const dataIndex = key;
            // key = key.substring(key.indexOf('-') + 1, key.length) + '月';
            arr.push({
              align: 'center',
              title: key,
              dataIndex: dataIndex,
            });
          }
        });
        arr.push({
          align: 'center',
          title: '总计',
          dataIndex: 'total',
        });
        arr.push({
          align: 'center',
          title: '对比BP差异',
          dataIndex: 'different',
        });
        return arr;
      } else {
        return [];
      }
    },
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {},
};
</script>

<style lang="less" scoped>
.table-list-container {
  margin-top: 20px;
}
</style>
